package com.ghsc.ghxc.jsb.ui.integral.activity

import androidx.lifecycle.lifecycleScope
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.getCompatColor
import com.cn.appcore.ext.gone
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.visible
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.widget.CustomRefreshListener
import com.cn.appcore.widget.RefreshHelper
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivityIntegralOrderDetailBinding
import com.ghsc.ghxc.jsb.ext.copyToClipboard
import com.ghsc.ghxc.jsb.ext.intentExtras
import com.ghsc.ghxc.jsb.ext.setOnFastClickListener
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketViewModel
import com.ghsc.ghxc.jsb.ui.integral.bean.IntegralOrderDetailData
import com.ghsc.ghxc.jsb.ui.integral.bean.IntegralOrderDetailResult
import com.ghsc.ghxc.jsb.ui.integral.view.SubDeliveryPopViiew
import com.ghsc.ghxc.jsb.view.ConfirmCancelPopViiew
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.scwang.smart.refresh.layout.api.RefreshLayout
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral.activity
 * 类名：IntegralOrderDetailActivity
 * 创建时间：2024/5/22 17:07
 * 创建人 @author： xiaolin
 * 描述 ：Todo...积分的订单详情
 */
class IntegralOrderDetailActivity : BaseVMActivity<IntegralSupermarketViewModel, ActivityIntegralOrderDetailBinding>()  {
    override fun attachLayoutRes() = R.layout.activity_integral_order_detail
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
       return  IntegralSupermarketViewModel::class.java
    }

    private val oid: String by intentExtras("oid","")
    private val type: Int by intentExtras("type",1)

    var orderDetailsBean : IntegralOrderDetailData?=null
    override fun initView() {
        binding.apply {
            bar.topTitle.text = "订单详情 "
            bar.topLeftView.setOnClickListener { finish() }
        }

        binding.apply {
            tvHome.setOnClickListener { finish() }
            stvCopy.setOnFastClickListener {
                orderDetailsBean?.let {
                    copyToClipboard(this@IntegralOrderDetailActivity,it.oid)
                }
            }
            stvLogisticsCopy.setOnFastClickListener {
//                orderDetailBean?.let {
//                    copyToClipboard(this@OrderDetailActivity,"${it.kdname}\r\n${it.kdno}","快递单号已复制")
//                }
            }

            stvTuiKuan.setSingleClickListener {
                //申请退款
                XPopup.Builder(this@IntegralOrderDetailActivity)
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                    .customHostLifecycle(lifecycle)
                    .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                    .asCustom(
                        ConfirmCancelPopViiew(this@IntegralOrderDetailActivity,"提示","确定要退款吗?",
                            btnConfirm={
                                val map: HashMap<String, Any> = HashMap()
                                map["oid"] = oid
                                GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onShopRefundMethod(it1) }
                            },
                            btnCancel={
                            },)
                    )
                    .show()
            }
            stvSub.setSingleClickListener {
                //确认收货
                if(type==1){
                    XPopup.Builder(this@IntegralOrderDetailActivity)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                        .customHostLifecycle(lifecycle)
                        .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                        .asCustom(
                            ConfirmCancelPopViiew(this@IntegralOrderDetailActivity,"提示","请您检查商品无误后再点击确认收货?",
                                btnConfirm={
                                    val map: HashMap<String, Any> = HashMap()
                                    map["oid"] = oid
                                    GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onShopConfirmMethod(it1) }
                                },
                                btnCancel={
                                },)
                        )
                        .show()
                }else{
                    //商家
                    mViewModel.onKuaiDiListMethod("")
                }

            }
        }
        initRefreshLayout()
    }
    private fun initRefreshLayout() {
        binding.refreshLayout.setEnableLoadMore(false)
        RefreshHelper.initRefreshLayout(binding.refreshLayout, object : CustomRefreshListener {
            override fun _onRefresh(refreshLayout: RefreshLayout?) {
                initData()
            }

            override fun _onLoadMore(refreshLayout: RefreshLayout?) {

            }
        })
    }
    override fun initData() {
        super.initData()
        val map: HashMap<String, Any> = HashMap()
        map["oid"] = oid.toString()
        GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onIntegralOrderDetailMethod(it1) }
    }
    var isCut=true  //是否正常拉起
    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            delay(1000) // 延时1秒
            // 在这里执行需要延时的操作
            if (!isCut){
                initData()
            }
        }

    }
    override fun initRequestObserver() {
        super.initRequestObserver()
        binding.apply {
            mViewModel.apply {
                errorMsg.observe(this@IntegralOrderDetailActivity) {
                    binding.refreshLayout.finishRefresh()
                    showToast(it)
                }
                againLoginMsg.observe(this@IntegralOrderDetailActivity) {
                    binding.refreshLayout.finishRefresh()
                    showToast(CustKey.IN_AGAIN_MSG)
                    LoginHelper.startLoginPage()
                }
                onIntegralOrderDetail.observe(this@IntegralOrderDetailActivity){
                    binding.refreshLayout.finishRefresh()
                    it.data.data.let {
                        orderDetailsBean = it
                        tvName.text = "${it.shouhuo_name} ${it.shouhuo_mobile}"
                        tvAddress.text = "${it.shouhuo_site}"
                        //
                        GlideUtil.ImageLoad(this@IntegralOrderDetailActivity,it.skuimages,ivCommodityImg)
                        tvCommodityName.text = it.title
                        tvPayPrice.text = "${it.total_price}"
                        tvCount.text = "x${it.cnt}"
                        //
                        tvOrderNumber.text = "${it.oid}"
                        tvCreateTime.text = "${it.crt_time}"
                        tvOrderState.text = "${it.status}"
                        tvPriceTotal.text = "${it.total_price}"

                        if(type==1){
                            if(it.status == "待发货"){
                                stvTuiKuan.visible()
                                stvTuiKuan.text="退 款"
                            }else{
                                stvTuiKuan.gone()
                            }
                            if(it.status == "待收货"){
                                stvSub.visible()
                                stvSub.text="确认收货"
                            }else{
                                stvSub.gone()
                            }
                        }else{
                            if(it.status == "待发货"){
                                stvTuiKuan.gone()
                                stvSub.visible()
                                stvSub.text="订单发货"
                            }else{
                                stvTuiKuan.gone()
                                stvSub.gone()
                            }
                        }






                    }
                }
                onShopRefundData.observe(this@IntegralOrderDetailActivity){
                    showToast("操作成功")
                    initData()
                }
                onShopConfirmData.observe(this@IntegralOrderDetailActivity){
                    showToast("操作成功")
                    initData()
                }
                onShopFaHuoData.observe(this@IntegralOrderDetailActivity){
                    showToast("操作成功")
                    initData()
                }
                onKuaiDiListData.observe(this@IntegralOrderDetailActivity){
                    //快递
                    loge("快递快递"+it.data.list.size)
                    XPopup.Builder(this@IntegralOrderDetailActivity)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter  )
                        .customHostLifecycle(lifecycle)
                        .navigationBarColor(getCompatColor(R.color.bg_dark_color))
                        .asCustom(
                            SubDeliveryPopViiew(this@IntegralOrderDetailActivity,"订单发货",oid,it.data.list.toMutableList(),
                                btnConfirm = { kdno,kdname ->
                                    val map: HashMap<String, Any> = HashMap()
                                    map["oid"] = oid
                                    map["kdno"] = kdno
                                    map["kdname"] = kdname
                                    GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onShopFaHuoMethod(it1) }
                                },
                                btnCancel={
                                },)
                        )
                        .show()
                }
//                cancelOrderResult.observe(this@OrderDetailActivity){
//                    showToast("订单已取消")
//                    finish()
//                }
//                onTixingFahuo.observe(this@OrderDetailActivity){
//                    showToast("已提醒发货")
//                    binding.refreshLayout.autoRefresh()
//                }
//                //确认收货
//                onQuerenShouhuo.observe(this@OrderDetailActivity){
//                    showToast("确认收货成功")
//                    binding.refreshLayout.autoRefresh()
//                }
//                onShenqingTuikuan.observe(this@OrderDetailActivity){
//                    showToast("申请成功")
//                    binding.refreshLayout.autoRefresh()
//                }
            }
        }
    }
}